﻿package fpc.layouts;

class ALineLayout extends ALayout, implements ILineLayout
{
	public function new()
	{
		super();
		_components = [];
	}
	
	var _components:Array<ILayoutable>;
	
	public function add(component:ILayoutable)
	{
		_components.push(component);
		component.heraldResize.add(onComponentResize);
		heraldAddChild.dispatch(component);
	}
	
	public function remove(component:ILayoutable)
	{
		_components.remove(component);
		component.heraldResize.remove(onComponentResize);
		heraldRemoveChild.dispatch(component);
	}
	
	public function addBefore(component:ILayoutable, rightComponent:ILayoutable)
	{
		for (i in 0 ... _components.length)
		{
			if (_components[i] == rightComponent)
			{
				_components.insert(i, component);
				component.heraldResize.add(onComponentResize);
				heraldAddChild.dispatch(component);
				break;
			}
		}
	}
	
	public function addAfter(leftComponent:ILayoutable, component:ILayoutable)
	{
		for (i in 0 ... _components.length)
		{
			if (_components[i] == leftComponent)
			{
				_components.insert(i + 1, component);
				component.heraldResize.add(onComponentResize);
				heraldAddChild.dispatch(component);
				break;
			}
		}
	}
	
	public override function removeAll()
	{
		var i = _components.length;
		while (i-- > 0)
		{
			remove(_components[i]);
		}
	}
	
	public override function iterator()
	{
		return _components.iterator();
	}
}